<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/27 0027
 * Time: 15:24
 */

namespace app\admin\controller;


use think\Db;

class User extends Middle
{
    //todo 用户列表
    public function UserList(){
        $page = $this->page($this->data);
        $phone      = isset($this->data['phone']) ? $this->data['phone']:false;               
      	$map['a.phone'] = ['like','%'.$phone.'%'];
        $list = Db::name('user')
          	->alias('a')
          	->join('user_service b','a.uid=b.uid')
            ->where($map)
            ->order('create_time desc')
            ->limit($page['page'],$page['limit'])
            ->select();
        $count = Db::name('user')
          	->alias('a')
          	->join('user_service b','a.uid=b.uid')
            ->where($map)
            ->count();
        return $this->returnSuccess(['list'=>$list,'count'=>$count]);
    }

    //todo 详情
    public function UserDetail(){
        $map = ['uid'=>$this->data['uid']];
        $res = Db::name('user')
            ->where($map)
            ->find();
        if($res['pid'] > 0)$res['pid'] = Db::name('user')->where(['uid'=>$res['pid']])->value('nickname');
        if($res)return $this->returnSuccess($res);
        return $this->returnError($this->NoData);
    }
  
  
  	public function userSet(){
        $vip = isset($this->data['vip']) ? $this->data['vip'] : false;
        $expire = isset($this->data['expire']) ?$this->data['expire'] :false;
        $uid = isset($this->data['uid']) ? $this->data['uid'] : 0;
        $map = ['uid'=>$uid];
        if($vip)$map['vip'] = $vip;
        if($expire){
            $date = Db::name('user_service')->where(['uid'=>$uid])->value('expire');
            if($date < time())$date = time();
            $map['expire'] = $date + $expire*86400;
        }
        $res = Db::name('user_service')->update($map);
        if($res)return $this->returnSuccess('操作成功');
        return $this->returnError('操作失败');
    }
  
  	public function zhuxiao(){
    	$uid = $this->data['uid'];
      	$user = Db::name('user')->where(['uid'=>$uid])->find();
      	if(!$user)return $this->returnError('用户不存在');
      	$res = Db::name('user')->where(['uid'=>$uid])->update(['reg_id'=>null]);
      	if($res)return $this->returnSuccess('操作成功');
        return $this->returnError('操作失败');  
    
    }
}